package com.ggh.jinjilive.ui.home.voice.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemVoiceListBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.home.voice.VoiceHot;
import com.bumptech.glide.Glide;

import java.util.List;

public class VoiceAdapter1 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<VoiceHot> voiceHotList;

    public void setAdd(List<VoiceHot> voiceHotList) {
        this.voiceHotList = voiceHotList;
        notifyDataSetChanged();
    }


    public void Add(List<VoiceHot> voiceHotList) {
        if(null != this.voiceHotList){
            this.voiceHotList.addAll(voiceHotList);
        }else {
            this.voiceHotList = voiceHotList;
        }
        notifyDataSetChanged();
    }



    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemVoiceListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_voice_list, parent, false);
        return new ItemViewHolder(binding);
    }


    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
        if(recyclerViewholder instanceof ItemViewHolder){
            ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
            VoiceHot voiceHot = voiceHotList.get(position);
            if (null != voiceHot) {
                Glide.with(holder.binding.RoundedImageView.getContext()).load(voiceHot.getCover()).into(holder.binding.RoundedImageView);
                holder.binding.tvName.setText(voiceHot.getTitle());
                holder.binding.tvName.setSelected(true);
                holder.binding.tvNumber.setText(voiceHot.getNumber() +"");
                if(voiceHot.getIs_pass() == 1){
                    holder.binding.imgLocked.setVisibility(View.VISIBLE);
                }else {
                    holder.binding.imgLocked.setVisibility(View.GONE);
                }
            }
        }

    }


    @Override
    public int getItemCount() {
        return null == voiceHotList ? 0 : voiceHotList.size();
    }




    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemVoiceListBinding binding;
        public ItemViewHolder(@NonNull ItemVoiceListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            ViewGroup.LayoutParams params =  binding.llLive.getLayoutParams();
            params.height = ScreenUtils.getScreenWidth() / 2 - SizeUtils.dp2px(15);
            binding.llLive.setLayoutParams(params);
        }
    }
}
